package com.shushan.util;

import android.text.TextUtils;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @ClassName: DateUtils
 * @Desciption: 日期工具类
 * @author: yichaohua
 * @date: 2018-02-05
 */
public class DateUtils {

    public final static String defaultPattern = "yyyy-MM-dd HH:mm:ss";


    public static String getNowWeek() {
        Date date = new Date();
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);

        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0){
            w = 0;
        }

        return weekDays[w];
    }

    public static String createFileName(){
        Date nowDate = new Date( );
        SimpleDateFormat ft = new SimpleDateFormat ("yyyyMMddHHmmss");
        return ft.format(nowDate);
    }

    public static String getNowDate(){
        Date nowDate = new Date( );
        SimpleDateFormat ft = new SimpleDateFormat (defaultPattern);

        return ft.format(nowDate);
    }

    public static String getNowDate(String pattern){
        if(TextUtils.isEmpty(pattern)){
            pattern = defaultPattern;
        }

        Date nowDate = new Date( );
        SimpleDateFormat ft = new SimpleDateFormat (pattern);

        return ft.format(nowDate);
    }

    public static String getDate(String dateStr,String pattern){
        if(TextUtils.isEmpty(dateStr) || TextUtils.isEmpty(pattern)){
            return "";
        }

        SimpleDateFormat fromMat = new SimpleDateFormat(defaultPattern);
        Date date = null;
        try {
            date = fromMat.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        SimpleDateFormat toMat = new SimpleDateFormat(pattern);
        return toMat.format(date);
    }

    public static String getDate(String fromPattern,String dateStr,String pattern){
        if(TextUtils.isEmpty(fromPattern) || TextUtils.isEmpty(dateStr)
                || TextUtils.isEmpty(pattern)){
            return "";
        }

        SimpleDateFormat fromMat = new SimpleDateFormat(fromPattern);
        Date date = null;
        try {
            date = fromMat.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        SimpleDateFormat toMat = new SimpleDateFormat(pattern);
        return toMat.format(date);
    }
    //截取时间字符串的月日
    public static String captureMonthDay(String date){
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date target = sdf.parse(date);
            SimpleDateFormat format = new SimpleDateFormat("MM-dd");
            return format.format(target);
        }catch (ParseException pe){

        }
        return "";
    }
    //截取时间字符串的时分
    public static String captureTimeWithoutSecond(String date){
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date target = sdf.parse(date);
            SimpleDateFormat format = new SimpleDateFormat("HH:mm");
            return format.format(target);
        }catch (ParseException pe){

        }
        return "";
    }
    //按照format截取时间字符串
    public static String captureDateByFormat(String date,String formatStr){
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date target = sdf.parse(date);
            SimpleDateFormat format = new SimpleDateFormat(formatStr);
            return format.format(target);
        }catch (ParseException pe){

        }
        return "";
    }


    /**
     * 功能描述：返回小
     *
     * @return 返回小时(24小时制)
     */
    public static int getHour() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(System.currentTimeMillis()));
        return calendar.get(Calendar.HOUR_OF_DAY);
    }

    /**
     * 功能描述：返回分
     *
     * @return 返回分钟
     */
    public static int getMinute() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(System.currentTimeMillis()));
        return calendar.get(Calendar.MINUTE);
    }

    /**
     * 获取今天星期的索引值
     * @return
     */
    public static int getWeekIndex() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(System.currentTimeMillis()));
        return (calendar.get(Calendar.DAY_OF_WEEK) - 1);
    }

    /**
     * @param min 分钟数
     * @return 返回小时数字符串
     */
    public static String getHour(int min){
        float hour = (float) min/60;
        DecimalFormat df = new DecimalFormat("##0.0");
        return df.format(hour);
    }

    /**
     *
     * @param second 秒数
     * @return 返回小时数字符串
     */
    public static String getHourBySeconds(int second){
        return getHour(second/60);
    }

    public static int getHours(int startDay,int endDay){
        int nowDay = Integer.parseInt(getNowDate("yyyyMMdd"));
        if(nowDay-endDay>0){
            return (endDay - startDay)*24;
        }

        int days = nowDay - startDay;
        int hour = getHour();

        return ((days*24) + hour);
    }

    public static int getDays(int endDayte){
        int nowDate = Integer.parseInt(getNowDate("yyyyMMdd"));

        int days = endDayte - nowDate;
        if(days < 0){
            return 0;
        }else {
            return days;
        }
    }

    public static String getHourMinutes(int endDayte){
        int days = endDayte - Integer.parseInt(getNowDate("yyyyMMdd"));
        if(days < 0){
            return "00:00";
        }

        String hours = "";
        String minutes = "";

        int hour = 24-getHour();
        if(getMinute() > 0){
            hour -= 1;
        }

        if(hour < 10){
            hours = "0" + hour;
        }else {
            hours = "" + hour;
        }

        int min = 60 - getMinute();
        if(min < 10){
            minutes = "0" + min;
        }else {
            minutes = "" + min;
        }

        return (hours + ":" + minutes);
    }

}
